package com.lichongbing.lyoggl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lichongbing.lyoggl.model.Flags;
import com.lichongbing.lyoggl.model.Project;
import com.lichongbing.lyoggl.model.Tags;
import com.lichongbing.lyoggl.service.FlagsService;
import com.lichongbing.lyoggl.mapper.FlagsMapper;
import com.lichongbing.lyoggl.service.ProjectService;
import com.lichongbing.lyoggl.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class FlagsServiceImpl extends ServiceImpl<FlagsMapper, Flags>
    implements FlagsService{
   @Autowired
   private FlagsMapper flagsMapper;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private TagsService tagsService;

    @Override
    public List<Flags> getFlagsByUid(String userid) {
        return flagsMapper.getFlagsByUid(userid);
    }

    @Override
    public void addFlags(String userid, Flags flags) {

        Project project = new Project();
        Tags tags = new Tags();
        String id = UUID.randomUUID().toString().replace("-", "");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = dateFormat.format(new Date());
        String title = flags.getTitle();
        flags.setName(title+date);
        flags.setId(id);
        flags.setUid(userid);
        project.setName(title);
        project.setId(id);
        project.setUid(userid);
        projectService.save(project);
        tags.setName(title);
        tags.setId(id);
        tags.setUid(userid);
        tags.setB("1");
        tags.setR("1");
        tags.setG("1");
        tagsService.save(tags);
        flagsMapper.insert(flags);
    }

    @Override
    public void deleteFlags(String flagid) {
        flagsMapper.removeById(flagid);
    }

    @Override
    public void updateFlags(String fid, String active) {
        flagsMapper.updateFlags(fid,active);
    }


    @Override
    public Object getFlagsallByUid(String userid) {
        return flagsMapper.getFlagsallByUid(userid);
    }

    @Override
    public int getFlagsRowsByUid(String userid) {
        return flagsMapper.getFlagsRowsByUid(userid);
    }

    @Override
    public String getFlagsProjectNamebypid(String pid) {
        return flagsMapper.getFlagsProjectNamebypid(pid);
    }

    @Override
    public List<Flags> getFlagsByUidMain(String userid) {
        return flagsMapper.getFlagsByUidMain(userid);
    }
}
